<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="loadFormVisible = true">创建策略</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="归属" value="username"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 分类管理table -->
    <el-table :data="tableData" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="username" label="归属" min-width="120" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="batch" label="批次" min-width="80" align="center"></el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="package_name" label="包名" min-width="200" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" min-width="140" align="center" prop="create_time" label="创建时间">
        <template slot-scope="scope">{{scope.row.update_time| newDate}}</template>
      </el-table-column>
      <el-table-column min-width="150" align="center" prop="log" label="操作">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" type="primary" @click="getrunAppRestrictById(scope.row.id)">编辑</el-button>
            <el-button size="mini" @click="deleteShow(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>

    <el-dialog title="应用运行黑名单" :visible.sync="loadFormVisible" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <el-form :model="addForm" :rules="rules" ref="ruleForm" class="dialog_form">
        <el-form-item label="SN码批次:" label-width="100px" prop="batch">
          <el-select v-model="addForm.batch" placeholder="没有选中任何项" class="add_select">
            <el-option v-for="item in batchData" :key="item.id" :label="item.batch" :value="item.id.toString()"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="100px" label="包名:" v-for="(domain, index) in addForm.package_name" :key="domain.key" :rules="{ required: true, message: '请输入包名', trigger: 'blur' }"
          :prop="'package_name.' + index + '.value'">
          <div class="addPut">
            <el-input v-model="domain.value" placeholder="请输入包名"></el-input>
            <el-button class="dlePut btnLocation" round size="mini" icon="el-icon-delete" @click.prevent="removeDomain(domain)"></el-button>
          </div>
        </el-form-item>
        <el-form-item label-width="100px">
          <el-button type="primary" @click="addDomain" size="mini">添加包名</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="loadFormVisible = false" size="mini" round>取 消</el-button>
        <el-button v-if="editSubjects" type="primary" size="mini" round @click="getrunAppRestrictAdd('ruleForm')">确 定</el-button>
        <el-button v-else type="primary" @click="getrunAppRestrictEdit('ruleForm')" size="mini" round>编辑</el-button>
      </div>
    </el-dialog>
    <!-- 删除弹窗 -->
    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import { runAppRestrictList, runAppRestrictById, runAppRestrictAdd, runAppRestrictEdit, runAppRestrictDelete } from "@/api/deployment.js";
import tableMini from '@/utils/tableMini.js'
import dynamicTable from "@/components/DynamicTable";
import deleteDialog from "@/components/deleteDialog";
import { getBatchTwo } from "@/api/equipment.js";
export default {
  mixins: [tableMini],
  components: { dynamicTable, deleteDialog },
  data() {
    return {
      checkboxVal: ["归属", "批次", "包名", "创建时间"],
      tableData: [],
      select: "",
      listQuery: { startSize: 1, pageSize: 10, userId: localStorage.getItem("ID"), username: undefined },
      addForm: { batch: '', package_name: [{ value: '' }] },
      loadFormVisible: false,
      batchData: [],
      rules: {
        batch: [{ required: true, message: "选择批次", trigger: "blur" }]
      },
      subjectId: 0,
      editSubjects: true,
    }
  },
  created() {
    this.getTabularData()
    this.getBatchTwos()
  },
  methods: {
    addDomain() {
      if (this.addForm.package_name.length == 20) return this.$message({ type: 'warning', message: '需要定义包名不能超过20条' })
      this.addForm.package_name.push({ value: '', key: Date.now() });
    },
    removeDomain(item) {
      var index = this.addForm.package_name.indexOf(item)
      if (index !== -1) {
        this.addForm.package_name.splice(index, 1)
      }
    },
    //根据ID获取批次列表
    getBatchTwos(userId = localStorage.getItem("ID")) {
      getBatchTwo({ userId: userId }).then(res => {
        this.batchData = res.data
      }).catch(err => {
        this.batchData = []
      })
    },
    getrunAppRestrictById(id) {
      this.editSubjects = false
      this.subjectId = id
      runAppRestrictById({ id: id }).then(({ data }) => {
        this.loadFormVisible = true
        this.addForm.batch = data.batch
        this.addForm.package_name = data.package_name.split(',').map(item => { return { 'value': item } })
      }).catch(err => { })
    },
    //编辑
    getrunAppRestrictEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.id = this.subjectId
          addForm.package_name = this.addForm.package_name.map(item => item.value).toString()
          addForm.userId = localStorage.getItem("ID")
          runAppRestrictEdit(addForm).then(res => {
            this.loadFormVisible = false
            this.$message.success(res.msg)
            this.getTabularData()
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), id: this.deleteDialogs.deleteId }
      runAppRestrictDelete(params).then(res => {
        this.$message.success(res.msg)
        this.getTabularData()
      })
    },
    getrunAppRestrictAdd(fromName) {
      this.$refs[fromName].validate(valid => {
        if (valid) {
          let addForm = JSON.parse(JSON.stringify(this.addForm))
          addForm.userId = localStorage.getItem("ID")
          addForm.package_name = this.addForm.package_name.map(item => item.value).toString()
          runAppRestrictAdd(addForm).then(res => {
            this.$message.success(res.msg)
            this.getTabularData()
            this.loadFormVisible = false
          }).catch(err => { })
        } else {
          return false
        }
      })
    },
    getTabularData() {
      runAppRestrictList(this.listQuery).then(res => {
        this.tableData = res.data
        this.total = res.total
      }).catch(err => {
        this.tableData = []
        this.total = 0
      })
    },
    addFormclose() {
      this.$nextTick(() => {
        this.addForm = this.$options.data().addForm
        this.editSubjects = true
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 2px;
  font-size: 16px;
  cursor: pointer;
  user-select: none;
  color: #dcdfe6;
}
.searchBox {
  border-radius: 2px;
  margin-bottom: 10px;
  background: #f5f5f5;
  padding: 20px;
}
.appName {
  background-color: #409eff;
  color: #ffffff;
  border: 1px solid #409eff;
  border-radius: 4px;
  padding: 0 20px;
  white-space: nowrap;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  line-height: 28px;
  font-size: 12px;
  border-right: none;
  height: 28px;
  border-radius: 4px 0 0 4px;
}
</style>